#ifndef FKDATAMODEL_H
#define FKDATAMODEL_H

#include <QAbstractItemModel>
#include <QStack>

#include "fkdatamodels_global.h"

class FKDataRecord;

struct FKModelData{
    FKModelData(){}
    FKModelData(const FKModelData& other):
                            role(other.role),
                            data(other.data),
                            addressRows(other.addressRows),
                            addressProps(other.addressProps){}
    FKModelData(const int& role,const QVariant& data,
                const QStack<int> &addressRows,const QStack<int> &addressProps):
                            role(role),data(data),
                            addressRows(addressRows),addressProps(addressProps){}
    int role;
    QVariant data;
    QStack<int> addressRows;
    QStack<int> addressProps;
private:
    static int sInit;
    static int declareFKMetaType();
};
Q_DECLARE_METATYPE(FKModelData)

QDataStream& operator<<(QDataStream& stream,const FKModelData &data);
QDataStream& operator>>(QDataStream& stream,FKModelData &data);

class FKDATAMODELSSHARED_EXPORT FKDataModel : public QAbstractItemModel{
    Q_OBJECT
public:
    FKDataModel(QObject *parent = 0);
    ~FKDataModel();
    void initialize();
    QVariant retriveData()const;
    void installNewSource(const QVariant& source);
    QVariant data(const QModelIndex &index, int role) const;
    bool setData(const QModelIndex &index, const QVariant &value, int role);
    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    QModelIndex index(int row, int column, const QModelIndex &parent) const;
    QModelIndex parent(const QModelIndex &child) const;
    QHash<int,QByteArray> roleNames() const;
public slots:
    void updateData(FKModelData d);
signals:
    void updateDataRequest(FKModelData d);
protected:
    void setRootRecord(FKDataRecord* record);
    FKDataRecord* rootRecord()const{return _root;}
    FKDataRecord* record(const QModelIndex& index)const;
    void notifyDataChange(const QModelIndex& index,const QVariant& value,const int& role);
private:
    virtual void installNewSource(FKDataRecord* root);
    virtual void initialization();
    FKDataRecord* _root;
    bool _initialized;
};

#endif // FKDATAMODEL_H
